<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:24 
 * 类说明
 */
class ShopAction extends BaseAction{
	
	/**
	 * @authority 浏览
	 */
	public function index(){
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$UserModel = M("User");
		$SchoolAreaModel = M("SchoolAreas");
		$ShopProductModel = M("ShopProduct");
		$map = $this->loadSeachCondition();
		$map['flag'] = 1;
		$map['create_userid'] = $this->uid;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		for($i=0;$i<count($data);$i++){
			$user = $UserModel->where("id=".$data[$i]['user_id'])->field("username")->find();
			if($user){
				$data[$i]['username'] = $user['username'];
			}else{
				$data[$i]['username'] = "无";
			}
			$SchoolArea = $SchoolAreaModel->where("id=".$data[$i]['school_area_id'])->field("area_name")->find();
			if($SchoolArea){
				$data[$i]['area_name'] = $SchoolArea['area_name'];
			}else{
				$data[$i]['area_name'] = "无";
			}
			$ShopProductCount = $ShopProductModel->where("flag=1 and shop_id=".$data[$i]['id'])->count();
			$data[$i]['num'] = $ShopProductCount;

		}
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	// 新增
	public function add(){
		$ProvinceModel = M('ZhProvince');$CityModel = M('ZhCity');$AreaModel = M('ZhArea');$School = M("School");
		$schoolList = $School->field("id,name")->where(array('flag'=>1))->select();
		$province = $ProvinceModel->select();
		$city = $CityModel->where("province_id=".$province[0][id])->select();
		$area = $AreaModel->where("city_id=".$city[0][id])->select();
		$this->assign("schoolList",$schoolList);
		$this->assign("province",$province);
		$this->assign("city",$city);
		$this->assign("area",$area);
		$this->display();
	}
	
	/**
	 * 获取城市
	 */
	public function getCity(){
		$CityModel = M('ZhCity');$AreaModel = M('ZhArea');
		$province = $_REQUEST["province"];
		$city = $CityModel->where("province_id=".$province)->select();
		$area = $AreaModel->where("city_id=".$city[0][id])->select();
		$vo[0] = $city;
		$vo[1] = $area;
		$this->ajaxReturn($vo,1,0);
	}
	
	/**
	 * 获取区
	 */
	public function getArea(){
		$AreaModel = M('ZhArea');
		$city = $_REQUEST["city"];
		$vo = $AreaModel->where("city_id=".$city)->select();
		$this->ajaxReturn($vo,1,0);
	}
	
	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$user = M("User")->where(array("username"=>$_POST['username']))->field("id")->find();
		if(empty($user)){
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
		$data = $Model->create();
		$map['flag'] = 1;
		$map['create_userid'] = $this->uid;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		$data['user_id'] = $user['id'];
		$data['create_userid'] = $this->uid;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			M("User")->where(array('id'=>$user['id']))->setField('is_shop',1);
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	
	
	//修改店铺
	public function edit() {
		$Model = M($this->getActionName());$UserModel = M('User');$SchoolAreaModel = M("SchoolAreas");$School = M("School");
		$map['id'] = $_REQUEST['id'];
		$map['create_userid'] = $this->uid;
		$vo = $Model->where($map)->find();

		//学校区域信息
		$school_area_info = $SchoolAreaModel->where("id=".$vo['school_area_id'])->field("id,school_id")->find();
		$school_area = $SchoolAreaModel->where("school_id=".$school_area_info['school_id'])->field("id,area_name")->select();

		$school_floor = M("SchoolFloor")->where("school_id=".$school_area_info['school_id']." and flag=1")->field("floor_num")->select();

		$schoolList = $School->field("id,name")->where(array('flag'=>1))->select();
		//商家账号信息
		$user = $UserModel->where("id=".$vo['user_id'])->field("username")->find();
		$vo['username'] = $user['username'];
		$this->assign('school_area', $school_area);
		$this->assign('school_floor', $school_floor);
		$this->assign('username',$user['username']);
		$this->assign('schoolList',$schoolList);
		$this->assign('vo',$vo);
		$this->display();
	}
	
	/**
	 * 更新店铺
	 */
	public function update() {
		$Model = D($this->getActionName());

		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		//只能修改自己的店铺
		$map['id'] = $_REQUEST['id'];
		$map['create_userid'] = $this->uid;
		$vo = $Model->where($map)->find();
		if(empty($vo)){
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
		$param = $_REQUEST;
		$old_username = $param['old_username'];
		unset($param['old_username']);
		$user = M("User")->where(array("username"=>$param['username']))->field("id")->find();
		if(empty($user)){
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
		$old_user = M("User")->where(array("username"=>$old_username))->field("id")->find();
		$_REQUEST['user_id'] = $user['id'];
		// 更新数据
		if(false !== $Model->save($_REQUEST)) {
			//判断号码是否修改
			if($old_username!=$param['username']){
				M("User")->where(array('id'=>$user['id']))->setField('is_shop',1);
				if(!empty($old_user)){
					M("User")->where(array('id'=>$old_user['id']))->setField('is_shop',0);
				}
			}
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	/**
	 * 逻辑删除店铺
	 */
	public function logicDel(){
		$Model = M($this->getActionName());
		$ShopProductModel = M("ShopProduct");
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			//只能删除自己的店铺
			$map['id'] = $_REQUEST['id'];
			$map['create_userid'] = $this->uid;
			$vo = $Model->where($map)->find();
			if(empty($vo)){
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
			if (isset($id)) {
				$map['flag'] = 1;
				$map['shop_id'] = $id;
				$count = $ShopProductModel->where($map)->count();
				if($count != 0){
					$this->ajaxReturn(0,"删除失败！该店铺有上传商品！",0);
				}
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
	
	/**
	 * @authority 修改排序
	 */
	public function editSeq() {
		$Model = D($this->getActionName());
		$map['flag'] = 1;
		$map['create_userid'] = $this->uid;
		$data = $Model->where($map)->order('seq asc')->field('id,name as name')->select();
		$newData = array();
		$newData[0]['id'] = -1;
		$newData[0]['pId'] = 0;
		$newData[0]['name'] = "店铺";
		$newData[0]['isParent'] = true;
		$newData[0]['open'] = true;
		for($i=0; $i<count($data); $i++) {
			$newData[$i+1]['id'] = $data[$i]['id'];
			$newData[$i+1]['pId'] = $newData[0]['id'];
			$newData[$i+1]['name'] = $data[$i]['name'];
		}
		$this->assign("zNodes",json_encode($newData));
		$this->display();
	}
	
	/**
	 * @authority 更新排序
	 */
	public function updateSeq(){
		$Model = D($this->getActionName());



		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$where['create_userid'] = $this->uid;
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn(0,L('ADMIN_SEQ_ERROR'),0);
		}
	}

	/**
	 * @authority 修改店铺商品排序
	 */
	public function editProductSeq() {
		$Model = D("ShopProduct");
		$map['a.flag'] = 1;
		$map['a.shop_id'] = $_REQUEST['id'];
		$data =  $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->where($map)->order('a.seq asc')->field('a.id,b.name as name')->select();

		$newData = array();
		$newData[0]['id'] = -1;
		$newData[0]['pId'] = 0;
		$newData[0]['name'] = "店铺商品";
		$newData[0]['isParent'] = true;
		$newData[0]['open'] = true;
		for($i=0; $i<count($data); $i++) {
			$newData[$i+1]['id'] = $data[$i]['id'];
			$newData[$i+1]['pId'] = $newData[0]['id'];
			$newData[$i+1]['name'] = $data[$i]['name'];
		}

		$this->assign("zNodes",json_encode($newData));
		$this->display();
	}

	/**
	 * @authority 更新店铺商品排序
	 */
	public function updateProductSeq(){
		$Model = D("ShopProduct");
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn(0,L('ADMIN_SEQ_ERROR'),0);
		}
	}

	//检查学校是否存在
	public function checkSchool(){
		$param = $_REQUEST['param'];
		$user = M("School");
		$where['name'] = $param;
		$where['flag'] = 1;
		$count=$user->where($where)->count();
		if($count){
			echo 'y';
		}else{
			echo '此学校不存在';
		}
	}
	//检查用户是否存在
	public function checkUser(){
		$param = $_REQUEST['param'];
		$user = M("User");
		$where['username'] = $param;
		$where['flag'] = 1;
		if($_REQUEST['old_username']==$param){
			echo 'y';
			exit;
		}
		$userInfo=$user->where($where)->find();
		if($userInfo){
			$shop = M("Shop")->where(array("user_id"=>$userInfo['id']))->find();
			if($shop){
				echo '此账号已开店';
			}else{
				echo 'y';
			}
		}else{
			echo '此用户不存在';
		}
	}

	
	//检查店铺名是否可注册
	public function checkName(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		$where['name'] = $param;
		$where['flag'] = 1;
		$count=$user->where($where)->count();
		if($count){
			echo '学校名已被注册！';
		}else{
			echo 'y';
		}
	}
	
	//检查店铺名是否可注册，排除当前
	public function exCheckName(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		if(!empty($_REQUEST['id'])){
			$map['id'] = $_REQUEST['id'];
			$result = $user->where($map)->find();
			if($param == $result['name']) {
				echo 'y';
				return;
			}else{
				$where['name'] = $param;
				$where['flag'] = 1;
			}
		}else{
			$where['name'] = $param;
			$where['flag'] = 1;
		}
		$count=$user->where($where)->count();
		if($count){
			echo '学校名已被注册！';
		}else{
			echo 'y';
		}
	}
	
	/**
	 * 更改状态
	 */
	public function updateStatus() {
		//删除指定记录
		$Model = M($this->getActionName());
		$pk = $Model->getPk();
		$id = $_REQUEST[$pk];
		if (isset($id)) {
			$condition = array($pk => array('in', explode(',', $id)));
			$condition['create_userid'] = $this->uid;
			if (false !== $Model->where($condition)->setField($_REQUEST['field'],$_REQUEST['status'])) {
				$this->deleteSession();
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}

	
	private function deleteSession(){
		S('S_SYSTEM_SCHOOL',null);
	}


	public function schoolAreaList(){
		$SareasModel = M('SchoolAreas');
		$SchoolModel = M('School');
		$school_name = $_REQUEST['school_name'];
		$School = $SchoolModel->where(array('name'=>$school_name))->field("id,name")->find();
		$map['school_id'] = $School['id'];
		$map['flag'] = 1;
		$data = $SareasModel->where($map)->select();
		$this->ajaxReturn($data,1,0);
	}

	public function schoolFloorList(){
		$SchoolFloorModel = M('SchoolFloor');
		$SchoolModel = M('School');
		$school_name = $_REQUEST['school_name'];
		$School = $SchoolModel->where(array('name'=>$school_name))->field("id,name")->find();
		$map['school_id'] = $School['id'];
		$map['flag'] = 1;
		$data = $SchoolFloorModel->where($map)->select();
		$this->ajaxReturn($data,1,0);
	}

	//图片传图
	public function uploadImg(){
		$folderName = $_POST['folderName'];//格式  首字母大写
		$prefix = $_POST['prefix'];//格式  m_,s_
		$maxWidth = $_POST['maxWidth'];//格式500,200
		$maxHeight = $_POST['maxHeight'];//格式500,200
		import('ORG.Net.UploadFile');
		$targetFolder_b = './Uploads/'.$folderName.'/'.date('Y-m-d').'/';
		if(!is_dir($targetFolder_b)){
			@mkdir($targetFolder_b,0777);
		}
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  $targetFolder_b;// 设置附件上传目录
		if($maxWidth != 0 && $maxHeight != 0){
			$upload->thumb = true;
			$upload->thumbPrefix = $prefix;  //缩略图前缀
			$upload->thumbMaxWidth = $maxWidth;
			$upload->thumbMaxHeight = $maxHeight;
			$upload->thumbType = 0;//按设置大小截取 0 按原图等比例缩略
			$upload->thumbRemoveOrigin = true; //上传后移除原图
		}
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->ajaxReturn(0,$upload->getErrorMsg(),0);
		}else{// 上传成功
			$info =  $upload->getUploadFileInfo();
			//补上缩略图的前缀
			if($maxWidth != 0 && $maxHeight != 0){
				$bak['url'] = ltrim($info[0]['savepath'],".").$prefix.$info[0]['savename'];
			}else{
				$bak['url'] = ltrim($info[0]['savepath'],".").$info[0]['savename'];
			}
			$bak['localname'] = $info[0]['name'];
			$this->ajaxReturn($bak,'上传成功！',1);
		}
	}

	//店铺添加产品
	public function addProduct(){
		$Model = M($this->getActionName());
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$TypeModel = M("Type");
		$condition['reserve'] = 1;
		$condition['flag'] = 1;
		$types = $TypeModel->where($condition)->field("id,type_name as name")->order("seq asc")->select();
		$this->assign("types",$types);
		$this->assign("vo",$vo);
		$this->display();
	}

	//已添加产品列表
	public function getShopProductData(){
		$Model = M("ShopProduct");
		$map = $this->loadProductSeachCondition();
		$map['a.shop_id'] = $_REQUEST['id'];
		$map['a.flag'] = 1;

		$where = $this->loadSeachCondition();
		$total = $Model->where($where)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		if(!empty($page->sort)){
			if($page->sort=="is_attr desc" || $page->sort=="is_attr asc"){
				$sort = 'b.'.$page->sort;
			}else if($page->sort=="name desc" || $page->sort=="name asc"){
				$sort = 'b.'.$page->sort;
			}else if($page->sort=="seq desc" || $page->sort=="seq asc"){
				$sort = 'a.'.$page->sort;
			}else if($page->sort=="num desc" || $page->sort=="num asc"){
				$sort = 'a.'.$page->sort;
			}else if($page->sort=="id desc" || $page->sort=="id asc"){
				$sort = 'a.'.$page->sort;
			}
		}else{
			$sort = 'a.'.$page->sort;
		}
		$data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->field('a.id,a.product_id,b.name,a.num,b.is_attr')->where($map)->limit($page->firstRow,$page->listRows)->order($sort)->select();
		if($data){
			foreach($data as $p_key=>$p_v){
				if($p_v['is_attr']==1){
					$attribute_values = M("ShopAttributeValues")->where(array('shop_id'=>$_REQUEST['id'],'product_id'=>$p_v['product_id'],'flag'=>1))->select();
					if(!empty($attribute_values)){
						$data[$p_key]['attribute_values'] = $attribute_values;
					}else{
						$data[$p_key]['attribute_values'] = array();
					}
				}else{
					$data[$p_key]['attribute_values'] = array();
				}

			}
		}

		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}

	//产品列表
	public function getProductAllData(){
		$map['create_userid'] = $this->uid;
		$Model = M("Product");
		$map = $this->loadProductSeachCondition();
		$map['a.flag'] = 1;
		if($this->uid){
			$map['_string'] = 'create_userid=0 OR create_userid='.$this->uid;
		}else{
			$map['_string'] = 'create_userid=0';
		}

		$where = $this->loadSeachCondition();
		$where['flag'] = 1;
		$total = $Model->where($where)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		if(!empty($page->sort)){
			$sort = 'a.'.$page->sort;
		}else{
			$sort = $page->sort;
		}
		$data = $Model->alias('a')->join('left join __TYPE__ as b on b.id = a.type_id')->field('a.id,a.name,a.spec,a.desc,a.price,a.is_on,a.seq,a.create_time,b.type_name')->where($map)->limit($page->firstRow,$page->listRows)->order($sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadProductSeachCondition(){
		$map = array();
		if(!empty($_GET['name'])){
			$map['name'] = array("like","%".$_GET['name']."%");
		}
		if(!empty($_GET['type_id'])){
			$map['type_id'] = $_GET['type_id'];
		}
		return $map;
	}

	/**
	 * 添加至列表
	 */
	public function addToList(){
		$map['id'] = $_REQUEST["shop_id"];
		$map['create_userid'] = $this->uid;
		$total = M("Shop")->where($map)->count();// 查询满足要求的总记录数
		if($total<1){
			$this->ajaxReturn(0,"不能给别人店铺添加商品！",1);
		}
		$ShopProductModel = M('ShopProduct');
		$ShopTypeModel = M('ShopType');
		$ShopSubtypeModel = M('ShopSubtype');
		$AttributeValues = M('AttributeValues');
		$data['product_id'] = $_REQUEST["product_id"];
		$productInfo = M("Product")->field("type_id,subtype_code,type_id,is_attr")->where(array("id"=>$data['product_id']))->find();
		$data['shop_id'] = $_REQUEST["shop_id"];
		$data['subtype_code'] = $productInfo["subtype_code"];
		$data['type_id'] = $productInfo["type_id"];
		$data['flag'] = 1;

		$type_data['shop_id'] = $_REQUEST["shop_id"];
		$type_data['type_id'] = $productInfo["type_id"];
		$type_data['flag'] = 1;

		$subtype_data['shop_id'] = $_REQUEST["shop_id"];
		$subtype_data['subtype_code'] = $productInfo["subtype_code"];
		$subtype_data['flag'] = 1;

		//如果有规格   超市商品添加商品规格
		if($productInfo['is_attr']==1){
			$attr_data = $AttributeValues->where(array('product_id'=>$_REQUEST['product_id'],'flag'=>1))->select();
			if($attr_data){
				foreach($attr_data as $s_attr_key=>$s_attr_v){
					unset($attr_data[$s_attr_key]['id']);
					$attr_data[$s_attr_key]['attr_value_id']=$attr_data[$s_attr_key]['id'];
					$attr_data[$s_attr_key]['flag']=1;
					$attr_data[$s_attr_key]['num']=0;
					$attr_data[$s_attr_key]['shop_id']=$_REQUEST["shop_id"];
				}
			}
		}
		$count = $ShopProductModel->where($data)->count();
		$type_count = $ShopTypeModel->where($type_data)->count();
		$subtype_count = $ShopSubtypeModel->where($subtype_data)->count();
		if($count == 0){
			$ShopProductModel->add($data);
			M("ShopAttributeValues")->addAll($attr_data);
			if($type_count == 0){
				$ShopTypeModel->add($type_data);
			}
			if($subtype_count==0){
				$ShopSubtypeModel->add($subtype_data);
			}
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),0);
		}else{
			$this->ajaxReturn(0,"重复添加！",1);
		}
	}
	//删除店铺商品
	function del_shop_product(){
		$Model = M("ShopProduct");
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$condition = array($pk => array('in', $id));
				$condition["create_userid"]=$this->uid;
				if (false !== $Model->where($condition)->setField('flag',2)) {
					//$this->deleteSession();
					$id_arr = $id;
					$ShopTypeModel = M('ShopType');
					$ShopSubtypeModel = M('ShopSubtype');
					$product_id_arr = array();
					$shop_id = 0;
					foreach($id_arr as $s_p_key=>$s_p_id){
						$shop_product = $Model->where(array('id'=>$s_p_id,"create_userid"=>$this->uid))->field('product_id,shop_id,type_id,subtype_code')->find();
						$product_id_arr[] = $shop_product['product_id'];
						$shop_id = $shop_product['shop_id'];
						$shop_sub_p_count = $Model->where(array("shop_id"=>$shop_product['shop_id'],'subtype_code'=>$shop_product['subtype_code'],'flag'=>1,"create_userid"=>$this->uid))->count();
						if($shop_sub_p_count==0){
							$ShopSubtypeModel->where(array(array("shop_id"=>$shop_product['shop_id'],'subtype_code'=>$shop_product['subtype_code'])))->setField('flag',2);
						}
						$shop_type_p_count = $Model->where(array("shop_id"=>$shop_product['shop_id'],'type_id'=>$shop_product['type_id'],'flag'=>1,"create_userid"=>$this->uid))->count();
						if($shop_type_p_count==0){
							$ShopTypeModel->where(array(array("shop_id"=>$shop_product['shop_id'],'type_id'=>$shop_product['type_id'])))->setField('flag',2);
						}
					}
					//删除超市该产品的规格
					if(!empty($product_id_arr) && $shop_id!=0){
						M("ShopAttributeValues")->where(array('shop_id'=>$shop_id,'product_id' => array('in', $product_id_arr)))->delete();
					}
					//删除超市会场中的该产品
					if(!empty($product_id_arr) && $shop_id!=0){
						M("MeetingplaceProduct")->where(array('shop_id'=>$shop_id,'product_id' => array('in', $product_id_arr)))->delete();
					}
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}

	}
	//更新超市商品库存
	function updatenum(){
		$Model = M("ShopProduct");
		if (!empty($Model)) {
			$id = $_REQUEST['id'];
			$num = $_REQUEST['num'];
			if (isset($id)) {
				$condition = array(id => $id);
				$condition["create_userid"]=$this->uid;
				if (false !== $Model->where($condition)->setField('num',$num)) {
					$this->ajaxReturn(1,L('INDEX_UPDATE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('INDEX_UPDATE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('INDEX_UPDATE_ERROR'),0);
			}
		}
	}

	//更新超市商品库存
	function updateattrnum(){
		$Model = M("ShopProduct");
		if (!empty($Model)) {
			$id = $_REQUEST['id'];
			$attr_num = $_REQUEST['attr_num'];
			if (isset($id)) {
				if($attr_num){
					foreach($attr_num as $attr_key=>$attr_v){
						$attr_arr = explode(":",$attr_v);
						M("ShopAttributeValues")->where(array("id"=>$attr_arr[0]))->setField('num',$attr_arr[1]);
					}
				}
				$this->ajaxReturn(1,L('INDEX_UPDATE_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('INDEX_UPDATE_ERROR'),0);
			}
		}
	}
}